﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
using NDK.Module.Model;
using NDK.Utils;

namespace NDK.Module.DAL
{
    class LightServices
    {
        public void CreatLightTable(string tableName, SQLiteConnection conn)
        {
            string sql = "CREATE TABLE IF NOT EXISTS " + tableName + "(ID integer PRIMARY KEY UNIQUE NOT NULL,name varchar(50), com varchar(10) NOT NULL, chanel varchar(10) NOT NULL, value integer NOT NULL)";

            SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
            cmdCreateTable.ExecuteNonQuery();
        }

        /// <summary>
        /// 通过名称，得到一个对象实体
        /// </summary>
        public RseeLightController GetModelByName(string name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from Light ");
            strSql.Append(" where name=@name ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@name", DbType.String,50)};
            parameters[0].Value = name;

            RseeLightController model = new RseeLightController();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = Int16.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["name"].ToString();
                model.Type = ds.Tables[0].Rows[0]["type"].ToString();
                model.COM = ds.Tables[0].Rows[0]["com"].ToString();
                model.Chanel = (LightControlerChanel)Convert.ToInt16(ds.Tables[0].Rows[0]["chanel"].ToString());
                if (ds.Tables[0].Rows[0]["value"].ToString() != "")
                {
                    model.Value = Int16.Parse(ds.Tables[0].Rows[0]["value"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RseeLightController> GetAllModel()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Light ");
            DataSet ds = SQLiteUtils.Query(strSql.ToString());

            List<RseeLightController> lightList = new List<RseeLightController>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    RseeLightController model = new RseeLightController();
                    if (ds.Tables[0].Rows[i]["ID"].ToString() != "")
                    {
                        model.ID = Int16.Parse(ds.Tables[0].Rows[i]["ID"].ToString());
                    }
                    model.Name = ds.Tables[0].Rows[i]["name"].ToString();
                    model.Type = ds.Tables[0].Rows[i]["type"].ToString();
                    model.COM = ds.Tables[0].Rows[i]["com"].ToString();
                    model.Chanel = (LightControlerChanel)Convert.ToInt16(ds.Tables[0].Rows[i]["chanel"].ToString());
                    if (ds.Tables[0].Rows[i]["value"].ToString() != "")
                    {
                        model.Value = Int16.Parse(ds.Tables[0].Rows[i]["value"].ToString());
                    }
                    lightList.Add(model);
                }
            }
            else
            {
                return null;
            }
            return lightList;
        }
        #region generate

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Int32 ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Light");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = ID.ToString();

            return SQLiteUtils.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string Name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Light");
            strSql.Append(" where name=@Name ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@Name", DbType.String,20)};
            parameters[0].Value = Name;

            return SQLiteUtils.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(RseeLightController model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Light(");
            strSql.Append("ID,name,com,chanel,value,type)");
            strSql.Append(" values (");
            strSql.Append("@ID,@name,@com,@chanel,@value,@type)");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4),
                    new SQLiteParameter("@name", DbType.String,50),
                    new SQLiteParameter("@com", DbType.String,10),
                    new SQLiteParameter("@chanel", DbType.Int32,4),
                    new SQLiteParameter("@value", DbType.Int32,4),
                    new SQLiteParameter("@type", DbType.String,50),
            };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.COM;
            parameters[3].Value = Convert.ToInt32(model.Chanel);
            parameters[4].Value = model.Value;
            parameters[5].Value = model.Type;

            SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RseeLightController model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Light set ");
            strSql.Append("name=@name,");
            strSql.Append("com=@com,");
            strSql.Append("chanel=@chanel,");
            strSql.Append("value=@value");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@name", DbType.String,50),
                    new SQLiteParameter("@com", DbType.String,10),
                    new SQLiteParameter("@chanel", DbType.Int32,4),
                    new SQLiteParameter("@value", DbType.Int32,4),
                    new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.COM;
            parameters[2].Value = Convert.ToInt32(model.Chanel);
            parameters[3].Value = model.Value;
            parameters[4].Value = model.ID;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public bool UpdateEx(string name,string type, int value)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Light set ");
            strSql.Append("value=@value");
            strSql.Append(" where name=@name and type=@type");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@value", DbType.Int32,4),
                    new SQLiteParameter("@name", DbType.String,50),
                    new SQLiteParameter("@type", DbType.String,50)};

            parameters[0].Value = value;
            parameters[1].Value = name;
            parameters[2].Value = type;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Light ");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = ID;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Light ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = SQLiteUtils.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RseeLightController GetModel(Int16 ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,name,com,chanel,value from Light ");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = ID;

            RseeLightController model = new RseeLightController();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = Int16.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["name"].ToString();
                model.Type = ds.Tables[0].Rows[0]["type"].ToString();
                model.COM = ds.Tables[0].Rows[0]["com"].ToString();
                model.Chanel = (LightControlerChanel)Convert.ToInt16(ds.Tables[0].Rows[0]["chanel"].ToString());
                if (ds.Tables[0].Rows[0]["value"].ToString() != "")
                {
                    model.Value = Int16.Parse(ds.Tables[0].Rows[0]["value"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Light ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteUtils.Query(strSql.ToString());
        }
        #endregion

    }
}
